$(function(){
	$('#mcList').append('<div style="text-align: center;width:100%"><img src="/images/pic_spinner.gif" width="16" height="16" border="0" /></div>');
	getMcList();
});

function getMcList() {
	var url = '/json?action=QUERY_T_KEYWORD_BY_P_KEYWORD_ID_ACTION&P_KEYWORD_ID=100000000';
	$UI.listPage(url,'T_NAV_KEYWORD','#mcList',printMcRes,'<div style="text-align: center;width:100%">暂无数据</div>');
}

function printMcRes(obj,result,i,n,total) {
	var style = 'item fore' + (i+1);
	var divStr = [
		'<div class="',style,'">',
		'<span>',
		'<h3>',
		'<strong>',
		obj.KEYWORD_NAME,
		'</strong>'
	].join('');
	
	if (obj.KEYWORD_CHILDS.length > 0) {
		var childArray = [];
		$.each(obj.KEYWORD_CHILDS,function(c,e){
			if (e.PKeywordId == obj.KEYWORD_ID) {
				// 是否热门
				if (e.isHot == 1) {
					var name = '';
					if (e.isLight) {
						name = '<font color="#C00">'+e.keywordName+'</font>';
					} else {
						name = e.keywordName;
					}
					// 是否连接
					if (e.isLink == 1) {
						childArray.push(['<a href="/product/list.html?id=',e.keywordId,'&pid=',obj.KEYWORD_ID,'&level=2','&keyword=',encodeURI(e.keywordName),'&pkeyword=',encodeURI(obj.KEYWORD_NAME),'&fkey=',encodeURI(obj.KEYWORD_NAME),'">',name,'</a>'].join(''));
					} else {
						childArray.push(name);
					}
				}
			}
			var panel = '';
			// 宝宝营养
			if (e.PKeywordId == 101000000) {
				panel = '#foodType';
			// 宝宝扮靓
			} else if (e.PKeywordId == 102000000) {
				panel = '#dressType';
			// 宝宝用品
			} else if (e.PKeywordId == 103000000) {
				panel = '#articlesType';
			// 宝宝娱乐
			} else if (e.PKeywordId == 104000000) {
				panel = '#toyType';
			// 孕产妇
			} else if (e.PKeywordId == 105000000) {
				panel = '#msType';
			}
			if (panel) {
				$(panel+' >img').remove();
				if ($(panel+' >li').length < 9) {
					if (e.isLink == 1) {
						$(panel).append(['<li><a href="/product/list.html?id=',e.keywordId,'&pid=',e.PKeywordId,'&level=2','&keyword=',encodeURI(e.keywordName),'&pkeyword=',encodeURI(obj.KEYWORD_NAME),'&fkey=',encodeURI(obj.KEYWORD_NAME),'">',e.keywordName,'</a></li>'].join(''));
					}
				}
			}
		});
		divStr +=childArray.join(' ');
	}
	divStr += [
		'</h3>',
		'<s></s>',
		'</span>',
		'<div class="i-mc">'
	].join('');
	
	if (obj.KEYWORD_CHILDS.length > 0) {
		divStr += '<div class="subitem">';
		var count = 0;
		$.each(obj.KEYWORD_CHILDS,function(c,e){
			if (e.PKeywordId == obj.KEYWORD_ID) {
				// 是否热门
				if (e.isHot == 0) {
					count++;
					divStr += '<dl class="fore'+count+'">';
					// 是否高亮
					if (e.isLight == 1) {
						divStr += '<dt><font color="#000000">';
					} else {
						divStr += '<dt><font color="#C00">';
					}
					
					// 是否连接
					if (e.isLink == 1){
						divStr += [
							'<a href="/product/list.html?id=',
							e.keywordId,
							'&pid=',
							obj.KEYWORD_ID,
							'&level=2',
							'&keyword=',
							encodeURI(e.keywordName),
							'&pkeyword=',
							encodeURI(obj.KEYWORD_NAME),
							'&fkey=',
							encodeURI(obj.KEYWORD_NAME),
							'">',
							e.keywordName,
							'</a>'
						].join('');
					} else {
						divStr += e.keywordName;
					}
					divStr += [
						'</font></dt>',
						'<dd>'
					].join('');
					var id = e.keywordId;
					$.each(obj.KEYWORD_CHILDS,function(c,elem){
						if (elem.PKeywordId == id) {
							var name = '';
							if (elem.isLight) {
								name = '<font color="red">'+elem.keywordName+'</font>';
							} else {
								name = elem.keywordName;
							}
							if (elem.isLink == 1) {
								divStr += [
									'<em><a href="/product/list.html?id=',
									elem.keywordId,
									'&pid=',
									id,
									'&level=3',
									'&keyword=',
									encodeURI(elem.keywordName),
									'&pkeyword=',
									encodeURI(e.keywordName),
									'&fkey=',
									encodeURI(obj.KEYWORD_NAME+'/'+e.keywordName),
									'">',
									name,
									'</a></em>'
								].join('');
							} else {
								divStr += name;
							}
						}
					});
					
					divStr += [
						'</dd>',
						'</dl>'
					].join('');	
				}
			}
		});
		divStr += '</div>';
	}
	
	divStr += [
		'<div class="fr">',
		'<dl><dt>推荐商品</dt>',
		'<dd><ul>'
	].join('');

	var plateCode;
	switch(obj.KEYWORD_ID) {
		case 101000000:
			plateCode = 1021;
			break;
		case 102000000:
			plateCode = 1022;
			break;
		case 103000000:
			plateCode = 1023;
			break;
		case 104000000:
			plateCode = 1024;
			break;
		case 105000000:
			plateCode = 1025;
			break;
		default:
			break;
	}
	
	if (plateCode) {
		divStr += [
			'<li style="text-align:center;" id="li_',plateCode,'"><img src="/images/pic_loading.gif" /></li>'
		].join('');
	}
	
	divStr += [
		'</ul>',
		'</dd>',							
		'</dl>',
		'</div>'
	].join('');
//	
	divStr += [
		'</div>',
		'</div>'
	].join('');
	$(result).append(divStr);
	if (i+1 == total) {
		$('.item').hover(function() {
			$(this).toggleClass('hover');
		});
//		$(result).append('<div class="extra"><a href="#">全部商品分类</a></div>');
	}
	if (plateCode) {
		var url = '/json?action=QUERY_T_PLATE_PRODUCT_ACTION&PLATE_CODE='+plateCode;
		$UI.listPage(url,'T_PLATE_PRODUCT','#li_'+plateCode,printkeywordProductRes,'');
	}
}

function printkeywordProductRes(obj,result,i,n,total) {
	if (result && obj.PRODUCT_LIST.length>0) {
		var appendStr = '';
		$.each(obj.PRODUCT_LIST,function(j,m){
			appendStr += [
				'<li style="text-align:center;>',
				'<a href="/product/file.html?id=',
				m.productId,
				'">',
				'<img src="/upload/product/',
				m.productNo,
				'.jpg" width="160" height="160" style="cursor:pointer;" onclick="showFile(',m.productId,');"/>',
				'</a>',
				'<p>',
				'<a href="/product/file.html?id=',
				m.productId,
				'">',
				m.productName,
				'</a>',
				'</p>',
				'<p class="cDGray">金麒价：',
				'<strong class="cRed">￥',
				m.memberPrice,
				'</strong>',
				'</p></li>'
			].join('');
		});
		$(result).append(appendStr);
	}
}

function showFile(id) {
	window.location.href = '/product/file.html?id='+id;
}

function getCart(){
	var url = '/json?action=QUERY_CART_BY_MEMBER_ACTION';
	$.post(url,{},function(data){
		if(data.success){
			var status = data.root.STATUS.rs[0];
			var cart = data.root.CART.rs[0].PRODUCT_LIST;
			var items = data.root.CART.rs[0].ITEMS;
			var amount = data.root.CART.rs[0].AMOUNT;
			$('#cart_items').html(items);
			$('#cart_bar_items').html(items);
			$('#cart_bar_amount').html('￥'+ amount);
			var listNode = $('#cart_bar');
			listNode.html('');
			if(items == 0){
				$('#cart_bar').hide();
				$('#cart_bar_btn').hide();
				$('#empty_cart_bar').show();
				return;
			}
			$('#empty_cart_bar').hide();
			$('#cart_bar').show();
			$('#cart_bar_btn').show();
			$.each(cart,function(i, obj){
				var divStr = [
					'<li>',
						'<div class="p_detail">',
							'<p><strong>￥'+ (obj.MEMBER_PRICE).toFixed(2) +'</strong> x '+ obj.QTY +'</p>',
							'<p><a href="javascript:deleteProduct('+obj.PRODUCT_ID+');" class="blue">[删除]</a></p>',
							'<input type="hidden" id="AmountVlaue'+obj.PRODUCT_ID+'" name="AmountVlaue" value="'+obj.QTY+'"/>',
							'<input type="hidden" name="pid" value="'+obj.PRODUCT_ID+'" />',
						'</div>',
						'<div class="p_img"><a href="/product/file.html?id='+obj.PRODUCT_ID+'"><img src="/upload/product/'+obj.PRODUCT_NO+'.jpg" /></a></div>',
						'<div class="p_name"><a href="/product/file.html?id='+obj.PRODUCT_ID+'">'+ obj.PRODUCT_NAME +'</a></div>',
					'</li>'
				].join('');
				listNode.append(divStr);
			});
		}
	},'json');
}

getCart();

function updateCart(){
	var k = $("input[name='pid']");
	var v = $("input[name='AmountVlaue']");
	var elements = [];
	$.each(k, function(i, obj){
		var bo = {};
		bo.id = parseInt(obj.value);
		bo.qty = parseInt(v[i].value);
		elements.push(bo);
	});
	var url = '/json?action=UPDATE_PRODUCT_TO_CART_ACTION';
	$.post(url,{PRODUCT:JSON.stringify(elements)},function(data){
		if(data.success){
			getCart();
		}
	},'json');
}

function deleteProduct(id){
	$('#AmountVlaue'+ id).val(0);
	updateCart();
}